package com.gavin.plugin.database.help;

import com.gavin.model.Model;
import com.gavin.schema.dataservice.If;
import com.gavin.schema.dataservice.SQLIf;
import com.gavin.schema.dataservice.type.SQLIfTypeType;

public class ConditionUtils {
	
	public static boolean checkCondition(If ifItem, Model modelRequest){
		return checkCondition(ifItem.getValue1(), ifItem.getOperator().toString(), ifItem.getValue2(), ifItem.getType().getType(), ifItem.getType().toString(), modelRequest);
	}
	
	public static boolean checkCondition(SQLIf sqlIf, Model modelRequest){
		return checkCondition(sqlIf.getValue1(), sqlIf.getOperator().toString(), sqlIf.getValue2(), sqlIf.getType().getType(), sqlIf.getType().toString(), modelRequest);
	}
	
	/**
	 * 检查If的条件
	 * 
	 * @param strValue1
	 * @param strOperator
	 * @param strValue2
	 * @param strType
	 * @param modelRequest
	 * @return
	 * @throws Exception
	 */
	public static boolean checkCondition(String strValue1, String strOperator, String strValue2, int nType, String strType, Model modelRequest) {
		// =
		if (strOperator.equalsIgnoreCase("=")) {
			switch (nType) {
			case SQLIfTypeType.BOOLEAN_TYPE: {
				boolean value1 = ValueUtils.getBooleanValue(strValue1, modelRequest);
				boolean value2 = ValueUtils.getBooleanValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 == value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return value1.equals(value2);
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return value1.equals(value2);
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.equals(value2);
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return value1.equals(value2);
			}
			default: {
				throw new RuntimeException("Invalid type" + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase("!=")) {
			switch (nType) {
			case SQLIfTypeType.BOOLEAN_TYPE: {
				boolean value1 = ValueUtils.getBooleanValue(strValue1, modelRequest);
				boolean value2 = ValueUtils.getBooleanValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 != value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return !value1.equals(value2);
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return !value1.equals(value2);
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return !value1.equals(value2);
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return !value1.equals(value2);
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase(">")) {
			switch (nType) {
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 > value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return value1.compareTo(value2) > 0;
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return value1.compareTo(value2) > 0;
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) > 0;
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) > 0;
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase("<")) {
			switch (nType) {
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 < value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return value1.compareTo(value2) < 0;
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return value1.compareTo(value2) < 0;
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) < 0;
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) < 0;
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase(">=")) {
			switch (nType) {
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 >= value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return value1.compareTo(value2) >= 0;
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return value1.compareTo(value2) >= 0;
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) >= 0;
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) >= 0;
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase("<=")) {
			switch (nType) {
			case SQLIfTypeType.BYTE_TYPE: {
				byte value1 = ValueUtils.getByteValue(strValue1, modelRequest);
				byte value2 = ValueUtils.getByteValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.SHORT_TYPE: {
				short value1 = ValueUtils.getShortValue(strValue1, modelRequest);
				short value2 = ValueUtils.getShortValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.INTEGER_TYPE: {
				int value1 = ValueUtils.getIntegerValue(strValue1, modelRequest);
				int value2 = ValueUtils.getIntegerValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.LONG_TYPE: {
				long value1 = ValueUtils.getLongValue(strValue1, modelRequest);
				long value2 = ValueUtils.getLongValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.FLOAT_TYPE: {
				float value1 = ValueUtils.getFloatValue(strValue1, modelRequest);
				float value2 = ValueUtils.getFloatValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.DOUBLE_TYPE: {
				double value1 = ValueUtils.getDoubleValue(strValue1, modelRequest);
				double value2 = ValueUtils.getDoubleValue(strValue2, modelRequest);
				return value1 <= value2;
			}
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getStringValue(strValue1, modelRequest);
				String value2 = ValueUtils.getStringValue(strValue2, modelRequest);
				return value1.compareTo(value2) <= 0;
			}
			case SQLIfTypeType.DATE_TYPE: {
				String value1 = ValueUtils.getDateValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateValue(strValue2, modelRequest);
				return value1.compareTo(value2) <= 0;
			}
			case SQLIfTypeType.TIME_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) <= 0;
			}
			case SQLIfTypeType.DATETIME_TYPE: {
				String value1 = ValueUtils.getDateTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getDateTimeValue(strValue2, modelRequest);
				return value1.compareTo(value2) <= 0;
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase("EXIST")) {
			strValue1 = ValueUtils.getFieldId(strValue1);
			if (modelRequest.exists(strValue1) == true) {
				return true;
			} else {
				return false;
			}
		} else if (strOperator.equalsIgnoreCase("NOTEXIST")) {
			strValue1 = ValueUtils.getFieldId(strValue1);
			if (modelRequest.exists(strValue1) == false) {
				return true;
			} else {
				return false;
			}
		} else if (strOperator.equalsIgnoreCase("MATCH")) {
			switch (nType) {
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return value1.matches(value2);
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else if (strOperator.equalsIgnoreCase("NOTMATCH")) {
			switch (nType) {
			case SQLIfTypeType.STRING_TYPE: {
				String value1 = ValueUtils.getTimeValue(strValue1, modelRequest);
				String value2 = ValueUtils.getTimeValue(strValue2, modelRequest);
				return !value1.matches(value2);
			}
			default: {
				throw new RuntimeException("Invalid type " + strType);
			}
			}
		} else {
			throw new RuntimeException("Invalid operator " + strOperator);
		}
	}
	
}

